﻿using System;
using System.Web.Mvc;
using Yz.Core.Tools;

namespace Yz.Mvc.Filters
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
    public class ExceptionAttribute : FilterAttribute, IExceptionFilter
    {
        public virtual void OnException(ExceptionContext filterContext)
        {
            //将异常信息入队. 
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            YzNLog.Fatal(filterContext.Exception, "未定义的全局异常处理");
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = new JsonResult()
                {
                    Data = new { suc = false, msg = "出现未知错误" }
                };
            }
            else
            {
                // 跳转到错误页
                filterContext.Result = new RedirectResult("/Account/Error");
            }
        }
    }
}